//
//  timeViewController.m
//  GaKan
//
//  Created by hztech on 2017/12/26.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "timeViewController.h"
#import "JSDropDownMenu.h"
#import "WXDUse.h"

@interface timeViewController ()<JSDropDownMenuDataSource,JSDropDownMenuDelegate,UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic)JSDropDownMenu *menu;
@property(strong,nonatomic)UIButton *makesure;
@property(strong,nonatomic)UITableView *tableView;
@end
//全局变量
@implementation timeViewController
{
    NSMutableArray *_data2;
    NSInteger _currentData2Index;
    NSString *type;
    NSMutableArray *leaveDataOk;
    NSMutableArray *leaveDataNO;
}
//重写init
-(instancetype)init{
    self = [super init];
    if (self)
    {
        [self setToptittle:@"考勤电子单据"];
        [self setTopTittleColor:[UIColor whiteColor]];
        [self setTopBgColor:headColor];
        [self setBackBtnImage:[UIImage imageNamed:@"wo_merchandise_detail_back@3x"]];
        [self setBackBtnHide:NO];
            }
    return self;
}
//主函数
- (void)viewDidLoad {
    [super viewDidLoad];
    leaveDataOk = [NSMutableArray new];
    leaveDataNO = [NSMutableArray new];
    _data2=[NSMutableArray arrayWithObjects:@"已审核",@"待审核", nil];
    type=_data2[0];
    
    [self.view addSubview:self.menu];
    [self.view addSubview:self.makesure];
    
    [self makeUI];
    [self gain];
}
//ui界面构造
-(void)makeUI{
    UILabel *typelab = [[UILabel alloc]initWithFrame:CGRectMake(20, 80, 80, 40)];
    typelab.text = @"审核状态:";
    [self.view addSubview:typelab];
    
    UIColor *background=COLOR(26, 86, 124, 1.0);
    
    UILabel *lab1 = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.makesure.frame)+30, ScreenWidth/2-1, 40)];
    lab1.text = @"请假日期";
    lab1.textAlignment=NSTextAlignmentCenter;
    lab1.backgroundColor = background;
    lab1.textColor = [UIColor whiteColor];
    [self.view addSubview:lab1];
    
    UILabel *lab2 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lab1.frame)+1, CGRectGetMinY(lab1.frame), ScreenWidth/4-1, 40)];
    lab2.text = @"类型";
    lab2.textAlignment=NSTextAlignmentCenter;
    lab2.backgroundColor = background;
    lab2.textColor = [UIColor whiteColor];

    [self.view addSubview:lab2];
    UILabel *lab3 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lab2.frame)+1, CGRectGetMinY(lab1.frame), ScreenWidth/4-1, 40)];
    lab3.text = @"申请类型";
    lab3.textAlignment=NSTextAlignmentCenter;
    lab3.backgroundColor = background;
    lab3.textColor = [UIColor whiteColor];

    [self.view addSubview:lab3];

}
#pragma mark  -  获取数据
//假条数据获取
-(void)gain{
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"leave"];
    
    [bquery whereKey:@"user_id" equalTo:[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"]];
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        
        for (BmobObject *obj in array) {
            WXDUse *hxt =[WXDUse new];
            
            NSString* ss = [obj objectForKey:@"leave_isOK"];
            hxt.beg = [obj objectForKey:@"leave_start"];
            hxt.end = [obj objectForKey:@"leave_end"];
            hxt.type = [obj objectForKey:@"leave_type"];
            NSLog(@"ss==%@",ss);
            if ([ss isEqualToString:@"1"]) {
                hxt.state = @"已审核";
                [leaveDataOk addObject:hxt];
            }else{
                hxt.state = @"待审核";
                [leaveDataNO addObject:hxt];
            }
        }
//        NSLog(@"_datesLeaveOK==%@",leaveDataOk);
//        NSLog(@"_datesLeaveNO==%@",leaveDataNO);
        [self.view addSubview:self.tableView];
    }];
}
#pragma mark  -  懒加载
//@property(strong,nonatomic)UITableView *tableView;
//tableView控件
-(UITableView*)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.makesure.frame)+70, SCREENWIDTH, SCREENHEIGHT-CGRectGetMaxY(self.makesure.frame)-70)];
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _tableView.dataSource=self;
        _tableView.delegate=self;
    }
    return _tableView;
}

//查询按钮
-(UIButton*)makesure{
    if (!_makesure) {
        _makesure = [[UIButton alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(self.menu.frame)+30, ScreenWidth-60, 40)];
        [_makesure setTitle:@"查询" forState:UIControlStateNormal];
        _makesure.backgroundColor = [UIColor orangeColor];
        _makesure.titleLabel.font = [UIFont systemFontOfSize: 17.0];
        
        _makesure.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
        [_makesure setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_makesure addTarget:self action:@selector(query) forControlEvents:UIControlEventTouchDown];
        _makesure.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    return _makesure;
}
//按钮时间
-(void)query{
//    if ([type isEqualToString:_data2[0]]) {
//        type = _data2[1];
//    }else
//        type = _data2[0];
    [self.tableView reloadData];
}
//下拉菜单
-(JSDropDownMenu*)menu{
    if (!_menu) {
        _menu = [[JSDropDownMenu alloc] initWithOrigin:CGPointMake(ScreenWidth/4+20, 80) andHeight:40];
        _menu.indicatorColor = [UIColor colorWithRed:175.0f/255.0f green:175.0f/255.0f blue:175.0f/255.0f alpha:1.0];
        _menu.separatorColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0];
        _menu.textColor = [UIColor colorWithRed:83.f/255.0f green:83.f/255.0f blue:83.f/255.0f alpha:1.0f];
        _menu.dataSource = self;
        _menu.delegate = self;
    }
    return _menu;
}
#pragma mark  -  tableView协议
//假条展示界面协议代理
//Section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
//Rows
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([type isEqualToString:_data2[1]]) {
        return leaveDataNO.count;
    }
    return leaveDataOk.count;
}

//高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40;
}
//眉
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellId=[NSString stringWithFormat:@"cellId_%lu_%ld",(unsigned long)[_data2 indexOfObject:type],indexPath.row];
    UITableViewCell *cell;
    if (indexPath.section==0) {
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (cell==nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor=[UIColor whiteColor];
            //UI
            UILabel *lab1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth/2-1, 40)];
            lab1.textAlignment=NSTextAlignmentCenter;
            lab1.tag = 1000;
            [cell addSubview:lab1];
            UILabel *lab2 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lab1.frame)+1, 0, ScreenWidth/4-1, 40)];
            lab2.textAlignment=NSTextAlignmentCenter;
            lab2.tag = 1001;
            [cell addSubview:lab2];
            UILabel *lab3 = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lab2.frame)+1, 0, ScreenWidth/4-1, 40)];
            lab3.textAlignment=NSTextAlignmentCenter;
            lab3.tag = 1002;
            [cell addSubview:lab3];
            UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(SCREENWIDTH/2, 0, 1, 40)];
            view1.backgroundColor = graybackgroundColor;
            [cell addSubview:view1];
            
            UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(SCREENWIDTH*0.75, 0, 1, 40)];
            view2.backgroundColor = graybackgroundColor;
            [cell addSubview:view2];
            
            UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0, 40, SCREENWIDTH, 1)];
            view3.backgroundColor = graybackgroundColor;
            [cell addSubview:view3];
        }
        //数据
        WXDUse *hxt;
        if ([type isEqualToString:_data2[1]]) {
            hxt=leaveDataNO[indexPath.row];
        }else{
            hxt=leaveDataOk[indexPath.row];
        }
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy.MM.dd"];
        //NSDate转NSString
        //NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
        UILabel *lab1=[cell viewWithTag:1000];
        lab1.text = [NSString stringWithFormat:@"%@-%@",[dateFormatter stringFromDate:hxt.beg],[dateFormatter stringFromDate:hxt.end]];
        UILabel *lab2=[cell viewWithTag:1001];
        lab2.text = hxt.type;
        UILabel *lab3=[cell viewWithTag:1002];
        lab3.text = hxt.state;
        
    }
    
    return cell;
}
#pragma mark  -  menu协议
//下拉菜单控件协议代理
- (NSInteger)numberOfColumnsInMenu:(JSDropDownMenu *)menu {
    
    return 1;
}

-(BOOL)displayByCollectionViewInColumn:(NSInteger)column{
    
    return YES;
}

-(BOOL)haveRightTableViewInColumn:(NSInteger)column{
    
    return NO;
}

-(CGFloat)widthRatioOfLeftColumn:(NSInteger)column{
    
    return 1;
}

-(NSInteger)currentLeftSelectedRow:(NSInteger)column{
    
    return _currentData2Index;
}

- (NSInteger)menu:(JSDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column leftOrRight:(NSInteger)leftOrRight leftRow:(NSInteger)leftRow{
    
    return _data2.count;
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForColumn:(NSInteger)column{
    
    return _data2[0];
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForRowAtIndexPath:(JSIndexPath *)indexPath {
    
    return _data2[indexPath.row];
    
}

- (void)menu:(JSDropDownMenu *)menu didSelectRowAtIndexPath:(JSIndexPath *)indexPath {
    _currentData2Index = indexPath.row;
    type=_data2[indexPath.row];
}

@end
